package com.ruoyi.buss.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.buss.domain.*;
import com.ruoyi.buss.mapper.*;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.JsonHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author : ChenBei
 * Date : 2021/10/7 10:18
 * Description : 账务信息
 **/
@Service
public class AccountingService extends ServiceImpl<AccountingMapper, Accounting> {

    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private IncomeMapper incomeMapper;
    @Autowired
    private OtherMapper otherMapper;
    @Autowired
    private RestaurantMapper restaurantMapper;
    @Autowired
    private ScenicSpotMapper scenicSpotMapper;
    @Autowired
    private VehiclesVesselsMapper vehiclesVesselsMapper;
    @Autowired
    private FilesMapper filesMapper;

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    public List<AccountingVO> list(AccountingVO vo)
    {
        return getBaseMapper().listPage(vo);
    }

    /**
     * 详情
     *
     */
    public AjaxResult detailById(String id)
    {
        Accounting detail = getById(id);
        AccountingVO vo = JsonHelper.o2m(detail,AccountingVO.class);
        vo.setHotelList( hotelMapper.selectList(new QueryWrapper<Hotel>().eq("accounting_id",vo.getId())));
        vo.setOtherList( otherMapper.selectList(new QueryWrapper<Other>().eq("accounting_id",vo.getId())));
        vo.setRestaurantList( restaurantMapper.selectList(new QueryWrapper<Restaurant>().eq("accounting_id",vo.getId())));
        vo.setScenicSpotList( scenicSpotMapper.selectList(new QueryWrapper<ScenicSpot>().eq("accounting_id",vo.getId())));
        vo.setVehiclesVesselsList( vehiclesVesselsMapper.selectList(new QueryWrapper<VehiclesVessels>().eq("accounting_id",vo.getId())));
        vo.setIncomeList( incomeMapper.selectList(new QueryWrapper<Income>().eq("accounting_id",vo.getId())));
        vo.setFilesList( filesMapper.selectList(new QueryWrapper<Files>().eq("accounting_id",vo.getId())));

        if(detail != null){
            return AjaxResult.success(vo);
        }else {
            return AjaxResult.error("信息不存在");
        }
    }


    /**
     * 新增
     *
     */
    @Transactional
    public AjaxResult addInfo(AccountingVO vo)
    {
        Accounting temp = JsonHelper.o2m(vo, Accounting.class);
        boolean isSuccess = save(temp);

        if( vo.getHotelList().size() > 0 ){
            vo.getHotelList().stream().forEach(item ->{
                item.setAccountingId(temp.getId());
                hotelMapper.insert(item);
            });
        }
        if( vo.getVehiclesVesselsList().size() > 0 ) {
            vo.getVehiclesVesselsList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                vehiclesVesselsMapper.insert(item);
            });
        }
        if( vo.getIncomeList().size() > 0 ) {
            vo.getIncomeList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                incomeMapper.insert(item);
            });
        }
        if( vo.getOtherList().size() > 0 ) {
            vo.getOtherList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                otherMapper.insert(item);
            });
        }
        if( vo.getRestaurantList().size() > 0 ) {
            vo.getRestaurantList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                restaurantMapper.insert(item);
            });
        }
        if( vo.getScenicSpotList().size() > 0 ) {
            vo.getScenicSpotList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                scenicSpotMapper.insert(item);
            });
        }
        if( vo.getFilesList().size() > 0 ) {
            vo.getFilesList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                filesMapper.insert(item);
            });
        }

        if(isSuccess){
            return AjaxResult.success("创建成功");
        }else{
            return AjaxResult.error("创建失败");
        }
    }

    /**
     * 修改
     *
     */
    public AjaxResult updateInfo(AccountingVO vo)
    {
        Accounting temp = JsonHelper.o2m(vo,Accounting.class);
        boolean isSuccess = updateById(temp);

        hotelMapper.delete(new QueryWrapper<Hotel>().eq("accounting_id",vo.getId()));
        vehiclesVesselsMapper.delete(new QueryWrapper<VehiclesVessels>().eq("accounting_id",vo.getId()));
        incomeMapper.delete(new QueryWrapper<Income>().eq("accounting_id",vo.getId()));
        otherMapper.delete(new QueryWrapper<Other>().eq("accounting_id",vo.getId()));
        restaurantMapper.delete(new QueryWrapper<Restaurant>().eq("accounting_id",vo.getId()));
        scenicSpotMapper.delete(new QueryWrapper<ScenicSpot>().eq("accounting_id",vo.getId()));
        filesMapper.delete(new QueryWrapper<Files>().eq("accounting_id",vo.getId()));

        if( vo.getHotelList().size() > 0 ){
            vo.getHotelList().stream().forEach(item ->{
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                hotelMapper.insert(item);
            });
        }
        if( vo.getVehiclesVesselsList().size() > 0 ) {
            vo.getVehiclesVesselsList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                vehiclesVesselsMapper.insert(item);
            });
        }
        if( vo.getIncomeList().size() > 0 ) {
            vo.getIncomeList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                incomeMapper.insert(item);
            });
        }
        if( vo.getOtherList().size() > 0 ) {
            vo.getOtherList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                otherMapper.insert(item);
            });
        }
        if( vo.getRestaurantList().size() > 0 ) {
            vo.getRestaurantList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                restaurantMapper.insert(item);
            });
        }
        if( vo.getScenicSpotList().size() > 0 ) {
            vo.getScenicSpotList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                scenicSpotMapper.insert(item);
            });
        }
        if( vo.getFilesList().size() > 0 ) {
            vo.getFilesList().stream().forEach(item -> {
                item.setAccountingId(temp.getId());
                item.setId(StrUtil.uuid().replace("-", ""));
                filesMapper.insert(item);
            });
        }

        if(isSuccess){
            return AjaxResult.success("更新成功");
        }else{
            return AjaxResult.error("更新失败");
        }
    }


    /**
     * 删除
     */
    public AjaxResult delInfoById(String id)
    {
        boolean isSuccess = removeById(id);
        hotelMapper.delete(new QueryWrapper<Hotel>().eq("accounting_id",id));
        vehiclesVesselsMapper.delete(new QueryWrapper<VehiclesVessels>().eq("accounting_id",id));
        incomeMapper.delete(new QueryWrapper<Income>().eq("accounting_id",id));
        otherMapper.delete(new QueryWrapper<Other>().eq("accounting_id",id));
        restaurantMapper.delete(new QueryWrapper<Restaurant>().eq("accounting_id",id));
        scenicSpotMapper.delete(new QueryWrapper<ScenicSpot>().eq("accounting_id",id));
        filesMapper.delete(new QueryWrapper<Files>().eq("accounting_id",id));
        if(isSuccess){
            return AjaxResult.success("删除成功");
        }else{
            return AjaxResult.error("删除失败");
        }
    }


}
